package org.itzixi.netty.utils;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;

public class CuratorConfig {
    public static String host = "192.168.208.130:2181";      // Zookeeper 地址
    public static Integer connectionTimeoutMs = 30 * 1000;   // 连接超时时间
    public static Integer sessionTimeoutMs = 3 * 1000;       // 会话超时时间
    public static Integer sleepMsBetweenRetry = 2 * 1000;    // 创建会话失败时，每次重试的间隔时间
    public static Integer maxRetries = 3;                    // 创建会话失败时，最大重试次数
    public static String namespace = "itzixi-im";            // 命名空间

    // CuratorFramework 客户端实例
    private static CuratorFramework client;

    static {
        // 重试策略：初始时间2秒，最大重试3次
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(sleepMsBetweenRetry, maxRetries);

        // 创建 CuratorFramework 客户端
        client = CuratorFrameworkFactory.builder()
                .connectString(host)
                .connectionTimeoutMs(connectionTimeoutMs)
                .sessionTimeoutMs(sessionTimeoutMs)
                .retryPolicy(retryPolicy)
                .namespace(namespace)
                .build();
        // 启动客户端
        client.start();
    }

    public static CuratorFramework getClient() {
        return client;
    }

}
